package com.fulihui.information.service;

import com.fulihui.information.api.ContentCommentsReplyService;
import com.fulihui.information.biz.content.ContentCommentsReplyBizManager;
import com.fulihui.information.core.db.CommentsOnOffDB;
import com.fulihui.information.core.repository.ContentCommentsReplyRepository;
import com.fulihui.information.dal.dataobj.InfoContentCommentsReply;
import com.fulihui.information.dto.InfoContentCommentsReplyDTO;
import com.fulihui.information.request.ContentCommentsReplyCreateRequest;
import com.fulihui.information.request.ContentCommentsReplyQueryRequest;
import com.fulihui.information.request.ContentCommentsReplyRequest;
import com.fulihui.information.request.ContentCommentsReplyUpdateRequest;
import org.apache.dubbo.config.annotation.Service;
import org.near.servicesupport.error.Errors;
import org.near.servicesupport.result.BaseResult;
import org.near.servicesupport.result.TPageResult;
import org.near.servicesupport.result.TSingleResult;
import org.near.toolkit.common.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import static com.fulihui.information.enums.ECommentsStatus.DELETE;
import static org.near.servicesupport.result.ResultBuilder.*;

/**
 * Created by LM on 2017/4/9.
 */
@Service(version = "1.0.0")
public class ContentCommentsReplyServiceImpl implements ContentCommentsReplyService {

    @Autowired
    private ContentCommentsReplyBizManager commentsReplyBizManager;
    @Autowired
    private ContentCommentsReplyRepository commentsReplyRepository;
    @Autowired
    private CommentsOnOffDB                commentsOnOffDB;

    @Override
    public TSingleResult<String> createComments(ContentCommentsReplyCreateRequest request) {
        String commentsReplyId = commentsReplyBizManager.createComments(request);
        if (StringUtil.isNotBlank(commentsReplyId)) {
            return succTSingle(commentsReplyId);
        } else {
            return failTSingle(Errors.Commons.SYSTEM_ERROR);
        }
    }

    @Override
    public BaseResult updateComments(ContentCommentsReplyUpdateRequest request) {
        InfoContentCommentsReply commentsReply = new InfoContentCommentsReply();
        commentsReply.setId(request.getCommentsId());
        commentsReply.setStatus(request.getStatus());
        commentsReply.setOperator(request.getOperator());
        boolean result = commentsReplyRepository.update(commentsReply);
        return result ? succ() : fail(Errors.Commons.SYSTEM_ERROR);
    }

    @Override
    public TPageResult<InfoContentCommentsReplyDTO> queryPage(ContentCommentsReplyQueryRequest request) {
        List<InfoContentCommentsReplyDTO> values = commentsReplyBizManager.queryPage(request);
        int count = (values != null && values.size() > 0) ? (int) commentsReplyBizManager
            .countByExample(request) : 0;
        return succTPage(values, request.getPage(), request.getRows(), count);
    }

    @Override
    public BaseResult removeCommentsReply(ContentCommentsReplyRequest request) {
        InfoContentCommentsReply commentsReply = new InfoContentCommentsReply();
        commentsReply.setId(request.getCommentsReplyId());
        commentsReply.setStatus(DELETE.getCode());
        commentsReply.setOperator(request.getOperator());
        boolean result = commentsReplyRepository.update(commentsReply);
        return result ? succ() : fail(Errors.Commons.SYSTEM_ERROR);
    }

    @Override
    public TSingleResult<InfoContentCommentsReplyDTO> queryByPK(ContentCommentsReplyRequest request) {
        InfoContentCommentsReplyDTO commentsReplyDTO = commentsReplyRepository
            .selectByPrimaryKey(request.getCommentsReplyId());
        return succTSingle(commentsReplyDTO);
    }

    @Override
    public TPageResult<InfoContentCommentsReplyDTO> queryPageExt(ContentCommentsReplyQueryRequest request) {
        // 查询评论显示开关
        boolean onOff = commentsOnOffDB.onOff();
        List<InfoContentCommentsReplyDTO> values = commentsReplyBizManager.queryPageExt(request,
            onOff);
        int count = 0;
        /*(values != null && values.size() > 0) ? (int) commentsReplyBizManager
        .countByExampleExt(request, onOff) : 0;*/
        return succTPage(values, request.getPage(), request.getRows(), count);
    }

    @Override
    public TPageResult<InfoContentCommentsReplyDTO> queryPageUserCommentsReply(ContentCommentsReplyQueryRequest request) {
        List<InfoContentCommentsReplyDTO> values = commentsReplyBizManager
            .queryPageUserCommentsReply(request);
        return succTPage(values, request.getPage(), request.getRows(), 0);
    }
}
